<template>
  <div class="app-container">
    <div class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="条码打印"
        :btn-list="btnList"
        :needReturn="false"
      >
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <!-- form -->
            <PageForm
              class="paddingBox order-model-formBox flex_wrap_start_start"
              v-model:ref-obj="formInfo.ref"
              :data="formInfo.data"
              :field-list="formInfo.fieldList"
              :rules="formInfo.rules"
              :label-width="formInfo.labelWidth"
            >
              <template v-slot:form-type>
                <el-select
                  @change="handleChangeType"
                  v-model="formInfo.data.type"
                  placeholder="请选择打印类型"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </template>
            </PageForm>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('detailInfo') }}</p>
          </div>
          <CodeBarPrint
            v-if="formInfo.data.type === 'productCode'"
            @handlePrintQRcode="onPrintQRcode"
            @callback="onCallbackTableInfo"
          >
          </CodeBarPrint>
          <ReplenishQRcode
            v-if="formInfo.data.type === 'rePrint'"
            @handlePrintQRcode="onPrintQRcode"
            @callback="onCallbackTableInfo"
          />
        </div>
      </NewFullScreen>
      <ChosePrinter
        v-model:visible="printerInfo.visible"
        @callBack="handlePrinterCallBack"
      />
    </div>
  </div>
</template>

<script setup name="printQRcode">
import {
  postPrintQrCodePrint,
  postGenerateToHis,
  productCodePrint,
} from '@/api/core'
import { getDictItemByDictCode } from '@/api/base'
import ReplenishQRcode from './replenish'
import CodeBarPrint from '@/views/product/qrcode/codeBarPrint'
const { proxy } = getCurrentInstance()
const changeList = ref([])
const tableInfo = ref({
  selections: [],
})
const options = ref([])
const typeCode = ref('print_code_type')
const btnList = ref([
  { label: '打印二维码', event: onBatchPrintQRcode, show: false },
  {
    label: '打印产品码',
    event: onBatchPrintProductCode,
    show: false,
  },
])
const showDialog = ref(true)
const formRefs = ref([])
const printerInfo = ref({
  visible: false,
  data: {},
})
const formInfo = ref({
  ref: null,
  data: {
    type: 'rePrint',
  },
  fieldList: [{ label: '选择类型', value: 'type', type: 'slot' }],
  rules: {
    deptName: [
      { required: true, message: '请选择打印类型', trigger: 'change' },
    ],
  },
  labelWidth: '120px',
})

function handleChangeType(type) {
  if (type === 'rePrint') {
    btnList.value[0].show = false
    btnList.value[1].show = false
  }
  if (type === 'productCode') {
    btnList.value[1].show = true
    btnList.value[0].show = false
  }
  tableInfo.value.selections = []
}
function onCallbackTableInfo(_tableInfo) {
  tableInfo.value = { ..._tableInfo }
}
function onGenerateCode(row) {
  if (!row.productionDate) row.productionDate = '1900-01-01'
  const params = new Array()
  params.push(row)
  postGenerateToHis(params).then((res) => {
    if (res && res.success) {
      proxy.$message({
        message: `${res.msg}`,
        type: 'success',
        duration: 3500,
        showClose: true,
      })
    }
  })
}
function onPrintQRcode(row) {
  if (!row.productionDate) row.productionDate = '1900-01-01'
  const params = new Array()
  params.push(row.id)

  postPrintQrCodePrint(params).then((res) => {
    if (res && res.success) {
      // params.forEach((item,index) => item.uniqueCode = res.data[index]);
      changeList.value = params
      printerInfo.value.visible = true
      printerInfo.value.data = res.data
      // this.$forceUpdate()
    } else {
      proxy.$message({
        message: `${res.msg}`,
        type: 'warning',
        duration: 3500,
        showClose: true,
      })
    }
  })
}
function onBatchPrintQRcode() {
  if (formInfo.value.data.type !== 'add') {
    let rest = tableInfo.value.selections[0]?.id || []
    tableInfo.value.selections.forEach(
      (item) => (rest = rest + ',' + item.groupConcatIds)
    )
    console.log(rest)
    const selectionsIds = rest.length && rest.split(',')
    // const ids = [].concat.apply([], selectionsIds);

    if (selectionsIds.length) {
      postPrintQrCodePrint(selectionsIds).then((res) => {
        if (res && res.success) {
          printerInfo.value.visible = true
          printerInfo.value.data = res.data
        }
      })
    } else {
      proxy.$message({
        message: '选择需要打印的二维码',
        type: 'warning',
        duration: 3500,
        showClose: true,
      })
    }
  }
}
function handlePrinterCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}
function getOptions() {
  getDictItemByDictCode(typeCode.value)
    .then((res) => {
      options.value = res.data
    })
    .catch((err) => {
      //err
      console.log(err)
    })
}
function onBatchPrintProductCode() {
  if (tableInfo.value.selections.length) {
    let data = tableInfo.value.selections.map((item) => item.id)
    productCodePrint(data).then((res) => {
      if (res && res.success) {
        printerInfo.value.visible = true
        printerInfo.value.data = res.data
      }
    })
  } else {
    proxy.$message({
      message: '选择需要打印的产品码',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
  }
}

onMounted(() => {
  getOptions()
  formRefs.value = [proxy.$refs['tableForm'], formInfo.value.ref]
})
</script>

<style scoped></style>
